<?php


namespace App\Common\RabbitMQ;


use AMQPConnectionException;
use Illuminate\Support\Facades\Log;

trait PublishTrait
{
    /**
     * @param array $msg
     * @param bool $persistent
     * @return bool
     */
    public static function publish(array $msg, bool $persistent = false): bool
    {
//        var_dump($persistent);die;

        $mq = new static($persistent);

        try {
            $ex = $mq->getExchange();
            $res = $ex->publish(json_encode($msg, JSON_UNESCAPED_UNICODE), $mq->getRoutingKey(), $mq->getPubFlags(), $mq->getAttributes());
        } catch (AMQPConnectionException $e) {
            Log::error($e->getMessage(), $e->getTrace());
            $mq->reconnection();
        } catch (\Throwable $e) {
            \Sentry\captureException($e);
            Log::error($e->getMessage(), $e->getTrace());
            $res = false;
        }
        return $res;
    }
}
